package com.project.service.activity;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.project.common.Constants;
import com.project.dao.activity.ActivityDao;
import com.project.dao.activity.ActivityGuestDao;
import com.project.entity.activity.Activity;
import com.project.entity.activity.ActivityGuest;
import com.project.service.BaseService;
import com.project.util.CurrentSessionUtil;
import com.project.util.GenerateCode;
import com.project.util.qrcode.QrcodeUtil;

@Transactional
@Service
public class ActivityService extends BaseService<Activity>{

	@Autowired
	private ActivityDao activityDao;
	@Autowired
	private ActivityGuestDao activityGuestDao;
	
	public List<Activity> getList(Activity activity){
		return activityDao.getList(activity);
	}
	
	public String saveEntity(Activity activity,String delIds){
		String result="true";
		try {
			if(activity.getId()==null){
				activity.setCreateTime(new Date());
				activity.setUpdateTime(new Date());
				activity.setCreateUser(CurrentSessionUtil.getCurrentUser().getUserCode());
				activityDao.save(activity);
	            //生成二维码
	            String url =  QrcodeUtil.createLocalQrcode(500, 500,Constants.CONTEXT_PATH+"#/ActivityDetail?id="+activity.getId(),"activity_qrcode"+GenerateCode.randomCode(4));
	            activity.setQrcodeUrl("/"+url);
	            activityDao.update(activity);
			}else{
				activity.setUpdateTime(new Date());
				activity.setUpdateUser(CurrentSessionUtil.getCurrentUser().getUserCode());
				activityDao.update(activity);
			}
			activityDao.saveOrUpdate(activity);
			
			if(StringUtils.isNoneBlank(delIds)){
				delIds = delIds.substring(0, delIds.length()-1);
				System.out.println("删除原有的特邀嘉宾："+delIds);
				String[] delIdss = delIds.split(",");
				for (String delId : delIdss) {
					activityGuestDao.delete(Long.valueOf(delId));
				}
			}
			
			List<ActivityGuest> guestList = activity.getActivityGuestList();
			if(guestList!=null){
				for (ActivityGuest activityGuest : guestList) {
					if(StringUtils.isNoneBlank(activityGuest.getName())){
						activityGuest.setActivityId(activity.getId());
						activityGuestDao.saveActivityGuest(activityGuest);
					}
				}
			}
//			result = activity.getPublishStatus().getValue();
		} catch (Exception e) {
			e.printStackTrace();
			result="false";
		}
		return result;
	}
	
	public Activity getById(Activity activity){
		Activity activity2 = this.activityDao.getById(activity.getId());
		if(activity2 != null){
			Activity activity3 = this.activityDao.getById(activity);
			activity2.setAdoptNum(activity3.getAdoptNum());
			activity2.setEnrollmentNum(activity3.getEnrollmentNum());
		}
		return activity2;
	}
	
	public void update(Activity activity){
		activity.setUpdateTime(new Date());
		activity.setUpdateUser(CurrentSessionUtil.getCurrentUser().getUserCode());
		activityDao.update(activity);
	}
}
